/*
 * Copyright (c) 2017. Rushang Co.Ltd All Rights Reserved.
 * 项目名称:rsdp-base
 * 文件名称:HtController.java
 * 功能简介:<br/>
 *
 * @create 2017-09-01
 * @author zhangjun
 */

package com.rshang.rsdp.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.rshang.rsdp.controller.BaseController;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.rshang.rsdp.bean.DataGrid;
import com.rshang.rsdp.bean.Response;
import com.rshang.rsdp.entity.TbUser;
import com.rshang.rsdp.service.IDmRsdlGsbService;
import com.rshang.rsdp.service.ITbRsdlRyxxService;
import com.rshang.rsdp.service.ITbRsdlTzjlService;
import com.rshang.rsdp.util.excel.ExcelView;

/**
 * 功能简介：合同数据处理api<br/>
 *
 * @author zhangjun
 * @create 2017年8月23日上午10:09:08
 */
@Controller
@RequiresAuthentication
@RequestMapping("/htxx")
public class HtController extends BaseController {

    @Autowired
    ITbRsdlRyxxService service;

    @Autowired
    ITbRsdlTzjlService tzjlService;
    
    @Autowired
    IDmRsdlGsbService gsbService;

    @ResponseBody
    @RequestMapping("/ybcx/manage")
    public DataGrid ybcxManage(HttpServletRequest request) {
        //修改为获取全部数据
        //return service.getYbcxList(getPage(), request, this.getSessionUser().getUserId(), this.getSubject().hasRole("业务经理"));
        return service.getYbcxList(getPage(), request, this.getSessionUser().getUserId(), true);
    }


    @RequestMapping("/ybcx/export")
    public ModelAndView ybcxExport(HttpServletRequest request, HttpServletResponse response) {
        //修改为获取全部数据
        //Workbook wb = service.ybcxListExport(request, this.getSessionUser().getUserId(), this.getSubject().hasRole("业务经理"));
        Workbook wb = service.ybcxListExport(request, this.getSessionUser().getUserId(), true);
        return new ModelAndView(new ExcelView("ybcx.xls", wb));
    }

    @ResponseBody
    @RequestMapping("/tzxx/manage")
    public DataGrid tzxxManage(HttpServletRequest request) {
        //修改为获取全部数据
        //return tzjlService.getManagePageList(getPage(), request, this.getSubject().hasRole("业务经理") ? "" : this.getSessionUser().getUserId());
        return tzjlService.getManagePageList(getPage(), request, "");
    }

    @RequestMapping("/tzxx/addSave")
    @ResponseBody
    public Response tzxxAddSave() {
        return tzjlService.addSave(request, this.getSessionUser().getUserId());
    }

    @RequestMapping("/tzxx/editSave")
    @ResponseBody
    public Response tzxxEditSave() {
        return tzjlService.editSave(request, this.getSessionUser().getUserId());
    }

    @RequestMapping("/tzxx/delete")
    @ResponseBody
    public Response delete() {
        return tzjlService.deleteSave(request, this.getSessionUser().getUserId());
    }

    @RequestMapping("/tzxx/export")
    public ModelAndView tzxxExport(HttpServletRequest request, HttpServletResponse response) {
        //修改为获取全部数据
        //Workbook wb = tzjlService.tzxxListExport(request, this.getSubject().hasRole("业务经理") ? "" : this.getSessionUser().getUserId());
        Workbook wb = tzjlService.tzxxListExport(request, "");
        return new ModelAndView(new ExcelView("tzxx.xls", wb));
    }

    @RequestMapping(value = "/tzxx/importSave", method = RequestMethod.POST)
    @ResponseBody
    public Response tzxxImportSave(@RequestParam("file1") MultipartFile file) {
        TbUser user = this.getSessionUser();
        return tzjlService.importSave(file, this.getSubject().hasRole("业务经理") ? "" : user.getUserId(), user);
    }

    @RequestMapping("/checkRyxm")
    @ResponseBody
    public Response checkRyxm() {
        return service.checkRyxm(request);
    }

    @RequestMapping("/addSave")
    @ResponseBody
    public Response ryxxAddSave() {
        return service.addSave(request, this.getSessionUser().getUserId());
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Response ryxxDeleteSave() {
        return service.deleteSave(request, this.getSessionUser().getUserId());
    }

    @RequestMapping("/editSave")
    @ResponseBody
    public Response ryxxEditSave() {
    		TbUser czr = this.getSessionUser();
        return service.editSave(request, czr.getUserId(),czr.getUserXm());
    }

    @RequestMapping("/export")
    public ModelAndView ryxxExport(HttpServletRequest request, HttpServletResponse response) {
        //修改为获取全部数据
        //Workbook wb = service.ryListExcelExport(request, this.getSubject().hasRole("业务经理")?"":this.getSessionUser().getUserId(),"");
        Workbook wb = service.ryListExcelExport(request, "", "");
        return new ModelAndView(new ExcelView("ryxx.xls", wb));
    }

    @ResponseBody
    @RequestMapping("/manage")
    public DataGrid ryxxManage(HttpServletRequest request) {
        //修改为获取全部数据
        //return service.getRyxxList(getPage(), request, this.getSubject().hasRole("业务经理") ? "" : this.getSessionUser().getUserId(), "");
        return service.getRyxxList(getPage(), request, "", "");
    }

    @RequestMapping("/rylzSave")
    @ResponseBody
    public Response ryxxLzSave(HttpServletRequest request) {
        return service.rylzSave(request, this.getSessionUser().getUserId());
    }
    
    @RequestMapping("/ryBgjsSave")
    @ResponseBody
    public Response ryBgjsSave(HttpServletRequest request) {
    		TbUser czr = this.getSessionUser();
        return service.ryBgjsSave(request, czr.getUserId(),czr.getUserXm());
    }

    @RequestMapping(value = "/importSave", method = RequestMethod.POST)
    @ResponseBody
    public Response htxxImportSave(@RequestParam("file1") MultipartFile file) {
        return service.ryxxImportSave(file, this.getSessionUser(), this.getSubject().hasRole("业务经理"));
    }
    
    @RequestMapping("/checkXgsmc2")
    @ResponseBody
    public Response checkXgsmc2() {
        return gsbService.checkXgsmc2(request);
    }
    
    @RequestMapping("/gsAddSave")
    @ResponseBody
    public Response gsAddSave() {
        return gsbService.gsAddSave(request, this.getSessionUser().getUserId());
    }
    
    @ResponseBody
    @RequestMapping("/tzxx/rycx")
    public DataGrid tzxxRycx(HttpServletRequest request) {
        return service.getRycxPageList(getPage(), request, "");
    }

    @RequestMapping("/cxwt")
    @ResponseBody
    public Response ryxxCxwtSave() {
        return service.cxwtSave(request, this.getSessionUser().getUserId());
    }
}
